﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iLapKan
{
    class NumberLP
    {
        public static char DecimalSymbol { get { return (1.5).ToString("#,0.0").Replace("1", "").Replace("5", "")[0]; } }
        public static char DigitGroupSymbol { get { return (1000).ToString("#,0").Replace("1", "").Replace("0", "")[0]; } }

        public static string fixNumber(object obj)
        {
            string number = StringLP.filterStr(obj.ToString(), "-0123456789" + DecimalSymbol);

            if (number.Contains("-"))
            {
                if (number.StartsWith("-")) number = "=" + number;
                number = number.Replace("-", "").Replace("=", "-");
            }

            if (number.Contains(DecimalSymbol + ""))
            {
                if (number.StartsWith(DecimalSymbol + "")) number = "0" + number;
                if (number.EndsWith(DecimalSymbol + "")) number = number.Substring(0, number.Length - 1);
                number = StringLP.Replace1time(number, DecimalSymbol + "", "*").Replace(DecimalSymbol + "", "").Replace("*", DecimalSymbol + "");
            }

            if ((number.IndexOf("-") == 0 && number.Length == 1) || number == "-0" || number.Length == 0) return "0";

            return number;
        }

        public static double getDouble(object obj)
        {
            if (obj == null) return 0;
            return Convert.ToDouble(fixNumber(obj.ToString()));
        }

        public static double getDouble(object obj, int decimalLength)
        {
            if (obj == null) return 0;
            string number = fixNumber(obj.ToString());
            int dotIndexer = number.IndexOf(DecimalSymbol);

            if (dotIndexer >= 0 && number.Length - dotIndexer > decimalLength + 1)
                number = number.Substring(0, dotIndexer + decimalLength + 1);
            return Convert.ToDouble(number);
        }

        public static int getInt(object obj)
        {
            if (obj == null) return 0;
            return Convert.ToInt32(fixNumber(obj.ToString()).Split(DecimalSymbol)[0]);
        }
    }
}